package cn.iocoder.yudao.module.ai.service.picturesort;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.picturesort.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.picturesort.PictureSortDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.picturesort.PictureSortMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 图片分类 Service 实现类
 *
 * @author zhangyujun
 */
@Service
@Validated
public class PictureSortServiceImpl implements PictureSortService {

    @Resource
    private PictureSortMapper pictureSortMapper;

    @Override
    public String createPictureSort(PictureSortSaveReqVO createReqVO) {
        // 插入
        PictureSortDO pictureSort = BeanUtils.toBean(createReqVO, PictureSortDO.class);
        pictureSortMapper.insert(pictureSort);
        // 返回
        return pictureSort.getId();
    }

    @Override
    public void updatePictureSort(PictureSortSaveReqVO updateReqVO) {
        // 校验存在
        validatePictureSortExists(updateReqVO.getId());
        // 更新
        PictureSortDO updateObj = BeanUtils.toBean(updateReqVO, PictureSortDO.class);
        pictureSortMapper.updateById(updateObj);
    }

    @Override
    public void deletePictureSort(String id) {
        // 校验存在
        validatePictureSortExists(id);
        // 删除
        pictureSortMapper.deleteById(id);
    }

    private void validatePictureSortExists(String id) {
        if (pictureSortMapper.selectById(id) == null) {
            throw exception(PICTURE_SORT_NOT_EXISTS);
        }
    }

    @Override
    public PictureSortDO getPictureSort(String id) {
        return pictureSortMapper.selectById(id);
    }

    @Override
    public PageResult<PictureSortDO> getPictureSortPage(PictureSortPageReqVO pageReqVO) {
        return pictureSortMapper.selectPage(pageReqVO);
    }

}